import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';

final netUrlReg = RegExp('^http');
final assetUrlReg = RegExp('^static');

class CommonImage extends StatelessWidget {
  final String src;
  final double width;
  final double height;
  final BoxFit fit;

  const CommonImage(
      {super.key,
      required this.src,
      required this.width,
      required this.height,
      this.fit = BoxFit.none});

  @override
  Widget build(BuildContext context) {
    if (netUrlReg.hasMatch(src)) {
      return CachedNetworkImage(
        width: width,
        height: height,
        imageUrl: src,
        progressIndicatorBuilder: (context, url, progress) => Center(
          child: CircularProgressIndicator(
            value: progress.progress,
          ),
        ),
        errorWidget: (context, url, error) => const Icon(Icons.error),
      );
    }
    if (assetUrlReg.hasMatch(src)) {
      return Image.asset(
        src,
        width: width,
        height: height,
        fit: fit,
      );
    }
    assert(false, '图片地址不合法！');
    return const Placeholder();
  }
}
